package com.hjqjl.whdemo.feature.feature0.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hjqjl.whdemo.base.BaseActivity;
import com.hjqjl.whdemo.databinding.ActivityHandlerBinding;

import java.lang.ref.WeakReference;

/**
 * handler的静态方法的示例，这样写防止内存泄漏
 */
public class HandlerActivity extends BaseActivity {
    public static void start(Context context) {
        Intent starter = new Intent(context, HandlerActivity.class);
        context.startActivity(starter);
    }

    private ActivityHandlerBinding binding;
    private Handler mHandler;
    private String mMessage;

    private static class MyHandler extends Handler {
        WeakReference<HandlerActivity> mWkActivity;

        public MyHandler(HandlerActivity activity) {
            super(Looper.getMainLooper());//我这里默认写主线程了
            mWkActivity = new WeakReference<>(activity);
        }


        public Activity getActivity() {
            return mWkActivity.get();
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //若Activity被回收，此时activity便为空
            HandlerActivity targetAct = mWkActivity.get();
            if (targetAct != null) {
                switch (msg.what) {
                    case 33: {
                        try {
                            String message = (String) msg.obj;
                            targetAct.binding.tvShow.setText(message + "----" + targetAct.mMessage + TimeUtils.getNowString());
                            targetAct.play();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    }
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityHandlerBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mHandler = new MyHandler(this);
        mMessage = "测试";
        binding.btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage(33, "handler传递的消息");
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mHandler == null) {
            mHandler = new MyHandler(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        mHandler = null;
    }

    private void play() {
        LogUtils.v("执行play方法");
        ToastUtils.showShort("执行play方法");
    }

    /***
     * 发送消息给消息处理handler
     */
    private void sendMessage(int what, Object obj) {
        try {
            Message message = new Message();
            message.what = what;
            message.obj = obj;
            mHandler.sendMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}